﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

#endregion

using ScrumTable.Common;

namespace ScrumTable.DL.Data.Generic.Cache
{
    /// <summary>
    /// Represents a work item link wrapper.
    /// </summary>
    public class CacheWorkItemLink : CacheElementBase
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the tfs id of the related element.
        /// </summary>
        /// <remarks>This is a non-tracked value.</remarks>
        public int RelatedId { get; set; }

        /// <summary>
        /// Gets the internal unique id of the related element.
        /// </summary>
        /// <remarks>This is a non-tracked value.</remarks>
        public string RelatedUnqiueId { get; set; }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new CacheWorkItemLink instance.
        /// </summary>
        /// <param name="uniqueId">Specifies the unique id of the work item link.</param>
        /// <param name="relatedId">Specifies the related work item tfs id.</param>
        /// <param name="relatedUniqueId">Specifies the related unqiue CacheWorkItem id.</param>
        internal CacheWorkItemLink(string uniqueId, int relatedId, string relatedUniqueId)
            : base(uniqueId, CacheIdentity.DefaultId, CacheIdentity.DefaultUniqueId, CacheIdentity.DefaultRevision)
        {
            PreCondition.AssertNotNullOrEmpty(relatedUniqueId, "relatedUniqueId");

            RelatedId = relatedId;
            RelatedUnqiueId = relatedUniqueId;
        }

        /// <summary>
        /// Creates a new CacheWorkItemLink instance.
        /// </summary>
        /// <param name="uniqueId">Specifies the unique id of the work item link.</param>
        /// <param name="relatedId">Specifies the related work item tfs id.</param>
        internal CacheWorkItemLink(string uniqueId, int relatedId)
            : base(uniqueId, CacheIdentity.DefaultId, string.Empty, CacheIdentity.DefaultRevision)
        {
            RelatedId = relatedId;
            RelatedUnqiueId = string.Empty;
        }

        private CacheWorkItemLink(string uniqueId)
            : base(uniqueId, CacheIdentity.DefaultId, string.Empty, CacheIdentity.DefaultRevision)
        {
            RelatedId = CacheIdentity.DefaultId;
            RelatedUnqiueId = CacheIdentity.DefaultUniqueId;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="left">Specifies the left object.</param>
        /// <param name="right">Specifies the right object.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(CacheWorkItemLink left, CacheWorkItemLink right)
        {
            // If both are null, or both are same instance, return true.
            if (ReferenceEquals(left, right))
            {
                return true;
            }

            // If one is null, but not both, return false.
            if ((object)left == null || (object)right == null)
            {
                return false;
            }

            // Return true if the fields match:
            return left.Equals(right);
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="left">Specifies the left object.</param>
        /// <param name="right">Specifies the right object.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(CacheWorkItemLink left, CacheWorkItemLink right)
        {
            return !(left == right);
        }

        /// <summary>
        /// Determines whether the specified CacheWorkItemLink is equal to the current object.
        /// </summary>
        /// <param name="toCompare">Parameter to check.</param>
        /// <returns>Returns true if the objects are equal.</returns>
        public override bool Equals(object toCompare)
        {
            // If parameter cannot be cast to DomainBase return false.
            return Equals(toCompare as CacheWorkItemLink);
        }

        /// <summary>
        /// Determines whether the specified CacheWorkItemLink is equal to the current object.
        /// </summary>
        /// <param name="obj">Parameter to check.</param>
        /// <returns>Returns true if the objects are equal.</returns>
        public bool Equals(CacheWorkItemLink obj)
        {
            // If parameter is null return false:
            if ((object)obj == null) // force compiler to use Object::operator==()
                return false;

            return (obj.RelatedUnqiueId == RelatedUnqiueId);
        }

        /// <summary>
        /// Returns the hash code for this CacheWorkItemLink.
        /// </summary>
        /// <returns>Returns the computed hash value.</returns>
        public override int GetHashCode()
        {
            return RelatedUnqiueId.GetHashCode();
        }

        /// <summary>
        /// Creates a new clone instance of the current object.
        /// </summary>
        /// <returns>Returns the created clone instance.</returns>
        protected override CacheElementBase CreateCloneInstance()
        {
            return new CacheWorkItemLink(UniqueId)
                       {
                           RelatedId = RelatedId,
                           RelatedUnqiueId = RelatedUnqiueId
                       };
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------
        #endregion
    }
}